package dao.impl;

import core.BaseDao;
import dao.BillDao;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import pojo.Bill;
import util.DruidUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class BillDaoImpl extends BaseDao implements BillDao {

    public Connection connection;

    public BillDaoImpl() {
        connection= DruidUtil.getConnection();
    }

    public void setConnection(Connection connection){
        this.connection=connection;
    }
    public Connection getConnection() {
        return connection;
    }

    /**
     * 增加账单信息
     * @param bill
     * @return
     */
    @Override
    public Integer addBill(Bill bill) {
        try {
            sql = "INSERT INTO bill(bill_time, bill_money, bill_type, bill_action, bill_rest) VALUES(?,?,?,?,?)";
            return queryRunner.update(sql,
                    new Timestamp(System.currentTimeMillis()),
                    bill.getBillMoney(),
                    bill.getBillType(),
                    bill.getBillAction(),
                    bill.getBillRest()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 增加账单信息
     * @param bill
     * @return
     */
    public Integer addBill(Bill bill, Connection connection) {
        try {
            sql = "INSERT INTO bill(bill_time, bill_money, bill_type, bill_action, bill_rest) VALUES(?,?,?,?,?)";
            return queryRunner.update(connection, sql,
                    new Timestamp(System.currentTimeMillis()),
                    bill.getBillMoney(),
                    bill.getBillType(),
                    bill.getBillAction(),
                    bill.getBillRest()
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取账单的数量
     * @param bill
     * @return
     */
    @Override
    public Integer countFindBillByParam(Bill bill) {
        try {
            sql = "SELECT COUNT(1) FROM bill WHERE bill_id>0";
            List<String> paramsList = new ArrayList<>();
            if (bill.getBillAction() !=null && !bill.getBillAction().isEmpty()){
                sql += " AND bill_action LIKE ?";
                paramsList.add("%"+bill.getBillAction()+"%");
            }
            if (bill.getBillType() != null && bill.getBillType() != -1){
                sql += " AND bill_type = ?";
                paramsList.add(bill.getBillType().toString());
            }

            if (paramsList.size()==2){
                return Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), paramsList.get(0), paramsList.get(1)).toString());
            }else if (paramsList.size()==1){
                return Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>(), paramsList.get(0)).toString());
            }else {
                return Integer.valueOf(queryRunner.query(sql, new ScalarHandler<>()).toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 根据条件查找账单
     * @param bill
     * @return
     */
    @Override
    public List<Bill> findBillByParam(Bill bill) {
        try {
            sql = "SELECT * FROM bill WHERE bill_id>0";
            List<String> paramsList = new ArrayList<>();
            if (bill.getBillAction() !=null && !bill.getBillAction().isEmpty()){
                sql += " AND bill_action LIKE ?";
                paramsList.add("%"+bill.getBillAction()+"%");
            }
            if (bill.getBillType() != null && bill.getBillType() != -1){
                sql += " AND bill_type = ?";
                paramsList.add(bill.getBillType().toString());
            }

            sql += " ORDER BY bill_id DESC LIMIT ?,?";

            Integer limit = bill.getLimit();
            Integer page = (bill.getPage()-1)*limit;

            if (paramsList.size()==2){
                return queryRunner.query(sql, new BeanListHandler<>(Bill.class, rowProcessor), paramsList.get(0), paramsList.get(1), page, limit);
            }else if (paramsList.size()==1){
                return queryRunner.query(sql, new BeanListHandler<>(Bill.class, rowProcessor), paramsList.get(0), page, limit);
            }else {
                return queryRunner.query(sql, new BeanListHandler<>(Bill.class, rowProcessor), page, limit);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }

    /**
     * 获取账单余额
     * @return
     */
    @Override
    public Double findBillRest() {
        try {
            sql = "SELECT bill_rest FROM bill ORDER BY bill_id DESC";
            Object query = queryRunner.query(sql, new ScalarHandler<>());
            if (query==null){
                return Double.valueOf(0);
            }else {
                return Double.valueOf(query.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0.00;
    }
}
